package com.tencent.bugly.beta.utils;

import android.util.Log;
import com.tencent.bugly.proguard.an;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

/* compiled from: BUGLY */
/* loaded from: classes.dex */
public class c {
    private static HashMap<Long, String> u = new HashMap<>();
    private String b;
    private com.tencent.bugly.beta.utils.a a = null;
    private long c = 0;
    private long d = 0;
    private HashMap<String, b> e = null;
    private byte[] f = new byte[16];
    private long g = 0;
    private long h = 0;
    private String i = null;
    private long j = 0;
    private long k = 0;
    private long l = 0;
    private long m = 0;
    private long n = 0;
    private long o = 0;
    private long p = 0;
    private long q = 0;
    private long r = 0;
    private long s = 0;
    private long t = 0;

    private long a(byte b2) {
        if (1 == b2) {
            return 32L;
        }
        return 2 == b2 ? 64L : 0L;
    }

    public c(String str) {
        this.b = null;
        this.b = str;
        u.put(3L, "x86");
        u.put(7L, "x86");
        u.put(8L, "mips");
        u.put(10L, "mips");
        u.put(40L, "armeabi");
        u.put(62L, "x86_64");
        u.put(183L, "arm64-v8a");
    }

    private String b() {
        return this.i;
    }

    private long c() {
        return this.m;
    }

    private long d() {
        return this.s;
    }

    private long e() {
        return this.t;
    }

    public synchronized String a() {
        if (!f()) {
            Log.e("ElfParser", "Failed to parseElfHeader elf header");
            return null;
        }
        String b2 = b();
        if (b2.equals("armeabi")) {
            if (!i()) {
                Log.e("ElfParser", "Failed to parseElfHeader section table");
                return b2;
            }
            b bVar = this.e.get(".ARM.attributes");
            if (bVar == null) {
                Log.e("ElfParser", "No .ARM.attributes section in the elf file");
                return b2;
            }
            return com.tencent.bugly.beta.utils.b.a(this.b, this.d, bVar.a());
        }
        return b2;
    }

    private static String a(long j, long j2) {
        String str = u.get(Long.valueOf(j));
        return (64 == j2 && str.equals("mips")) ? "mips64" : str;
    }

    private boolean f() {
        if (l()) {
            if (!g()) {
                j();
                return false;
            }
            j();
            return true;
        }
        return false;
    }

    private synchronized boolean g() {
        if (!h()) {
            Log.e("ElfParser", "Faile to parseElfHeader header indent of elf");
            return false;
        }
        try {
            this.g = this.a.g();
            this.h = this.a.g();
            this.i = a(this.h, this.c);
            this.j = this.a.h();
            if (32 == this.c) {
                long h = this.a.h();
                this.j = h;
                this.k = h;
                this.l = this.a.h();
                this.m = this.a.h();
            } else if (64 == this.c) {
                long i = this.a.i();
                this.j = i;
                this.k = i;
                this.l = this.a.i();
                this.m = this.a.i();
            } else {
                Log.e("ElfParser", "File format error");
                return false;
            }
            this.n = this.a.h();
            this.o = this.a.g();
            this.p = this.a.g();
            this.q = this.a.g();
            this.r = this.a.g();
            this.s = this.a.g();
            this.t = this.a.g();
            return true;
        } catch (IOException e) {
            Log.e("ElfParser", e.getMessage());
            return false;
        }
    }

    private synchronized boolean h() {
        if (!this.a.a(this.f)) {
            Log.e("ElfParser", "Fail to parseElfHeader elf indentification");
            return false;
        } else if (!a(this.f)) {
            Log.e("ElfParser", "Not a elf file: " + this.b);
            return false;
        } else {
            this.c = a(this.f[4]);
            if (0 == this.c) {
                Log.e("ElfParser", "File format error: " + ((int) this.f[4]));
                return false;
            }
            this.d = b(this.f[5]);
            if (com.tencent.bugly.beta.utils.a.a == this.d) {
                Log.e("ElfParser", "Endian error: " + ((int) this.f[5]));
                return false;
            }
            this.a.a(this.d);
            return true;
        }
    }

    private static boolean a(byte[] bArr) {
        return bArr.length >= 3 && Byte.MAX_VALUE == bArr[0] && 69 == bArr[1] && 76 == bArr[2] && 70 == bArr[3];
    }

    private long b(byte b2) {
        if (1 == b2) {
            return com.tencent.bugly.beta.utils.a.c;
        }
        if (2 == b2) {
            return com.tencent.bugly.beta.utils.a.b;
        }
        return com.tencent.bugly.beta.utils.a.a;
    }

    private synchronized boolean i() {
        this.e = a(c(), d(), e());
        return this.e != null;
    }

    private synchronized void j() {
        if (this.a == null) {
            return;
        }
        if (this.a.a()) {
            this.a = null;
        }
    }

    private synchronized boolean k() {
        if (this.a != null) {
            j();
        }
        try {
            this.a = new com.tencent.bugly.beta.utils.a(this.b, this.d);
        } catch (Exception e) {
            Log.e("ElfParser", e.getMessage());
            return false;
        }
        return true;
    }

    private synchronized boolean l() {
        if (this.a != null) {
            j();
        }
        try {
            this.a = new com.tencent.bugly.beta.utils.a(this.b);
        } catch (Exception e) {
            Log.e("ElfParser", e.getMessage());
            return false;
        }
        return true;
    }

    private synchronized HashMap<String, b> a(long j, long j2, long j3) {
        if (!k()) {
            j();
            return null;
        } else if (!this.a.b(j)) {
            j();
            return null;
        } else {
            HashMap<String, b> b2 = b(j2, j3);
            j();
            return b2;
        }
    }

    private synchronized HashMap<String, b> b(long j, long j2) {
        if (j <= 0 || j2 <= 0) {
            an.d("The SO file is invalid or has a shell.", new Object[0]);
            return null;
        }
        Vector vector = new Vector();
        for (int i = 0; i < j; i++) {
            vector.add(m());
        }
        a aVar = (a) vector.get((int) j2);
        long length = new File(this.b).length();
        an.c("File length = %d", Long.valueOf(length));
        if (aVar.c() >= length) {
            an.d("The SO file is invalid or has a shell.", new Object[0]);
            return null;
        }
        d dVar = new d(this.b, aVar.c(), aVar.d());
        HashMap<String, b> hashMap = new HashMap<>();
        Iterator it = vector.iterator();
        while (it.hasNext()) {
            a aVar2 = (a) it.next();
            String a2 = dVar.a(aVar2.a());
            b bVar = new b();
            bVar.a(a2);
            bVar.a(aVar2.b());
            bVar.b(aVar2.c());
            bVar.c(aVar2.d());
            bVar.d(aVar2.e());
            hashMap.put(a2, bVar);
        }
        dVar.a();
        return hashMap;
    }

    private synchronized a m() {
        a aVar = new a();
        try {
            aVar.a(this.a.h());
            aVar.b(this.a.h());
            if (32 == this.c) {
                aVar.c(this.a.h());
                aVar.e(this.a.h());
                aVar.d(this.a.h());
                aVar.f(this.a.h());
            } else if (64 == this.c) {
                aVar.c(this.a.i());
                aVar.e(this.a.i());
                aVar.d(this.a.i());
                aVar.f(this.a.i());
            } else {
                Log.e("ElfParser", "File format error");
                return null;
            }
            aVar.g(this.a.h());
            aVar.h(this.a.h());
            if (32 == this.c) {
                aVar.i(this.a.h());
                aVar.j(this.a.h());
            } else if (64 == this.c) {
                aVar.i(this.a.i());
                aVar.j(this.a.i());
            } else {
                Log.e("ElfParser", "File format error");
                return null;
            }
            return aVar;
        } catch (IOException e) {
            Log.e("ElfParser", e.getMessage());
            return null;
        }
    }

    /* compiled from: BUGLY */
    /* loaded from: classes.dex */
    public static class a {
        private long a = -1;
        private long b = 0;
        private long c = 0;
        private long d = -1;
        private long e = -1;
        private long f = 0;
        private long g = 0;
        private long h = 0;
        private long i = 0;
        private long j = 0;

        public long a() {
            return this.a;
        }

        public long b() {
            return this.d;
        }

        public long c() {
            return this.e;
        }

        public long d() {
            return this.f;
        }

        public long e() {
            return this.j;
        }

        public synchronized void a(long j) {
            this.a = j;
        }

        public synchronized void b(long j) {
            this.b = j;
        }

        public synchronized void c(long j) {
            this.c = j;
        }

        public synchronized void d(long j) {
            this.e = j;
        }

        public synchronized void e(long j) {
            this.d = j;
        }

        public synchronized void f(long j) {
            this.f = j;
        }

        public synchronized void g(long j) {
            this.g = j;
        }

        public synchronized void h(long j) {
            this.h = j;
        }

        public synchronized void i(long j) {
            this.i = j;
        }

        public synchronized void j(long j) {
            this.j = j;
        }
    }

    /* compiled from: BUGLY */
    /* loaded from: classes.dex */
    public static class b {
        private String a = null;
        private long b = -1;
        private long c = -1;
        private long d = 0;
        private long e = 0;

        public long a() {
            return this.c;
        }

        public synchronized void a(String str) {
            this.a = str;
        }

        public synchronized void a(long j) {
            this.b = j;
        }

        public synchronized void b(long j) {
            this.c = j;
        }

        public synchronized void c(long j) {
            this.d = j;
        }

        public synchronized void d(long j) {
            this.e = j;
        }
    }
}
